package com.tanhua.manager.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Movement;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.*;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.dubbo.api.mongo.MovementApi;
import com.tanhua.dubbo.api.mongo.VideoApi;
import org.apache.dubbo.config.annotation.Reference;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ManageService {

    @Reference
    private UserInfoApi userInfoApi;

    public ResponseEntity userList(Integer page, Integer pagesize, String city, String nickname, Long id) {

      PageResult pageResult = userInfoApi.queryUserList(page,pagesize,city,nickname,id);

      return ResponseEntity.ok(pageResult);

    }

    public ResponseEntity queryUserInfo(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);

        return ResponseEntity.ok(userInfo);

    }

    @Reference
    private VideoApi videoApi;

    public ResponseEntity queryUserVideo(Integer page, Integer pagesize, Long userId) {

      PageResult pageResult =  videoApi.queryVideoList(page,pagesize,userId);
        List<Video> items = (List<Video>) pageResult.getItems();
//        Video--> VideoVo
//       把Video转成 VideoVo
        List<VideoVo> list = new ArrayList<>();
        for (Video video : items) {
            VideoVo videoVo = new VideoVo();
            UserInfo userInfo = userInfoApi.findById(video.getUserId());
//            把 video+userInfo ---》videoVo
            BeanUtils.copyProperties(video,videoVo);

            videoVo.setId(video.getId().toString());
            videoVo.setCover(video.getPicUrl());
            videoVo.setSignature(video.getText());

            BeanUtils.copyProperties(userInfo,videoVo);
            videoVo.setUserId(userInfo.getId());
            list.add(videoVo);
        }

        pageResult.setItems(list);
        return ResponseEntity.ok(pageResult);

    }


    @Reference
    private MovementApi movementApi;
    public ResponseEntity queryUserMessages(Integer page, Integer pagesize, Long userId, Integer state) {

      PageResult pageResult =  movementApi.queryUserMovements(page,pagesize,userId,state);
      List<Movement> movementList = (List<Movement>) pageResult.getItems();
        List<MovementsVo> movementVoList = new ArrayList<>();
        for (Movement movement : movementList) {
            UserInfo userInfo = userInfoApi.findById(movement.getUserId());
            MovementsVo movementsVo = movementAndUserInfoToMovementVo(movement, userInfo);
            movementVoList.add(movementsVo);
        }
        pageResult.setItems(movementVoList);
        return ResponseEntity.ok(pageResult);

    }


    public MovementsVo movementAndUserInfoToMovementVo(Movement movement, UserInfo userInfo) {
        //            把UserInfo的内容放到MovementVo
        MovementsVo movementsVo = new MovementsVo();
        BeanUtils.copyProperties(userInfo,movementsVo);
        movementsVo.setUserId(userInfo.getId());
        if(userInfo.getTags()!=null){
            movementsVo.setTags(userInfo.getTags().split(","));
        }
//            把Movement的内容放到MovementVo
        BeanUtils.copyProperties(movement,movementsVo);
        movementsVo.setId(movement.getId().toString());//动态id //注意这个的id需要手动赋值
        movementsVo.setImageContent(movement.getMedias().toArray(new String[]{})); //集合转成字符串数据
        movementsVo.setDistance("500米以内"); //先随便给一个值

//        这里有个坑 API文档上要的是一个Integer数字 但是这里要的是一个字符串 "yyyy-MM-dd HH:mm"
        movementsVo.setCreateDate(new DateTime(movement.getCreated()).toString("yyyy-MM-dd HH:mm"));


        return movementsVo;
    }

    public ResponseEntity queryMovementById(String movementId) {
        Movement movement = movementApi.queryMovementById(movementId);
        UserInfo userInfo = userInfoApi.findById(movement.getUserId());
        MovementsVo movementsVo = movementAndUserInfoToMovementVo(movement, userInfo);

        return ResponseEntity.ok(movementsVo);

    }

    @Reference
    private CommentApi commentApi;

    public ResponseEntity queryMovementComment(Integer page, Integer pagesize, String movementId) {
        PageResult pageResult = commentApi.queryComments(page, pagesize, movementId);
        List<Comment> items = (List<Comment>) pageResult.getItems();
        //      把一个一个Comment转成CommentVo
        CommentVo commentVo = null;
        List<CommentVo> list = new ArrayList<>();
        for (Comment comment : items) {
            commentVo = new CommentVo();
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());

            commentVo.setNickname(userInfo.getNickname());
            commentVo.setAvatar(userInfo.getAvatar());

            commentVo.setId(comment.getId().toString());
            commentVo.setContent(comment.getContent());
            commentVo.setCreateDate(new DateTime(comment.getCreated()).toString("HH:mm") );
            commentVo.setLikeCount(comment.getLikeCount()); //当前评论的点赞数据

            list.add(commentVo);
        }
        pageResult.setItems(list);

        return ResponseEntity.ok(pageResult);


    }
}
